<!-- 侧边栏菜单项组件 -->
<template>
  <div v-if="!item.hidden && !item.meta?.hidden">
    <!-- 没有子菜单或只有一个可显示的子菜单 -->
    <template v-if="hasOneShowingChild(item.children, item)">
      <app-link v-if="onlyOneChild.meta" :to="resolvePath(onlyOneChild.path)">
        <el-menu-item :index="resolvePath(onlyOneChild.path)">
          <el-icon v-if="onlyOneChild.meta.icon">
            <component :is="onlyOneChild.meta.icon" />
          </el-icon>
          <template #title>{{ onlyOneChild.meta.title }}</template>
        </el-menu-item>
      </app-link>
    </template>

    <!-- 有子菜单的情况 - 支持多级 -->
    <el-sub-menu v-else ref="subMenu" :index="resolvePath(item.path)" popper-append-to-body>
      <template #title>
        <el-icon v-if="item.meta && item.meta.icon">
          <component :is="item.meta.icon" />
        </el-icon>
        <span>{{ item.meta.title }}</span>
      </template>

      <!-- 递归渲染子菜单 -->
      <sidebar-item
        v-for="child in item.children"
        :key="child.path"
        :item="child"
        :base-path="resolvePath(item.path)"
      />
    </el-sub-menu>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { isExternal } from '@/utils/validate'
import AppLink from './Link.vue'

// 定义 props
const props = defineProps({
  item: {
    type: Object,
    required: true
  },
  basePath: {
    type: String,
    default: ''
  }
})

// 用于存储唯一子菜单
const onlyOneChild = ref(null)

// 判断是否只有一个显示的子菜单
const hasOneShowingChild = (children = [], parent) => {
  if (!children) {
    children = []
  }
  
  const showingChildren = children.filter(item => {
    if (item.hidden || item.meta?.hidden) {
      return false
    } else {
      // 临时设置
      onlyOneChild.value = item
      return true
    }
  })

  // 当只有一个子菜单时直接显示
  if (showingChildren.length === 1) {
    return true
  }

  // 没有子菜单时显示父菜单
  if (showingChildren.length === 0) {
    onlyOneChild.value = { ...parent, noShowingChildren: true }
    return true
  }

  return false
}

// 解析路由路径
const resolvePath = (routePath) => {
  // 处理外部链接
  if (isExternal(routePath)) {
    return routePath
  }
  if (isExternal(props.basePath)) {
    return props.basePath
  }

  // 如果路径为空，返回基础路径
  if (!routePath) {
    return props.basePath
  }

  // 如果是绝对路径，直接返回
  if (routePath.startsWith('/')) {
    return routePath
  }

  // 如果没有基础路径，直接返回路由路径
  if (!props.basePath) {
    return `/${routePath}`
  }

  // 规范化路径
  const normalizedBase = props.basePath.replace(/\/+$/, '')
  const normalizedRoute = routePath.replace(/^\/+/, '')

  // 检查路由路径是否已经包含在基础路径中
  const baseParts = normalizedBase.split('/').filter(Boolean)
  const routeParts = normalizedRoute.split('/').filter(Boolean)

  // 检查最后一个路径段是否重复
  if (baseParts[baseParts.length - 1] === routeParts[0]) {
    routeParts.shift() // 移除重复的部分
  }

  // 构建最终路径
  const finalPath = routeParts.length > 0 
    ? `${normalizedBase}/${routeParts.join('/')}` 
    : normalizedBase

  console.log('Path resolution:', {
    basePath: props.basePath,
    routePath,
    finalPath,
    baseParts,
    routeParts,
    item: props.item
  })

  return finalPath
}
</script>

<style scoped>
.el-menu-item, .el-sub-menu {
  user-select: none;
}
</style> 